五、通过unity系统自带接口绑定按键事件
第一步:通过Hierarchy面板创建button
第二步:创建一个名为EventHandler的脚本,代码如下
public class EventHandler : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler,
IPointerExitHandler, IPointerDownHandler, IDragHandler
{
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.pointerId == -1)
Debug.Log("Left Mouse Clicked");
if (eventData.pointerId == -2)
Debug.Log("Right Mouse Clicked");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Pointer Enter");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Pointer Exit");
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Pointer Down");
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragged");
}
}
第三步,绑定EventHandler脚本到Button上
然后运行点击按钮就能看到运行的结果了。
第六种方式